package com.goods.business.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ListMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductStockServices;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/9  21:30
 * @description：TODO
 */
@Service
@SuppressWarnings("all")
public class ProductStockServicesImpl implements ProductStockServices {
    @Autowired(required = false)
    private ProductStockMapper productStockMapper;
    @Autowired(required = false)
    private ListMapper listMapper;
    //物资所有的库存信息
    @Override
    public PageVO<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize, ProductVO productVO) {
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //模糊查询
        Example o = new Example(Product.class);
        //根据名称查询
        String name = productVO.getName();
        //根据分类查询
        Long[] categorys = productVO.getCategorys();

        Example.Criteria criteria = o.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (categorys != null && categorys.length > 0) {
            for (int i = 0; i < categorys.length; i++) {
                switch (i) {
                    case 0:
                        criteria.andEqualTo("oneCategoryId", categorys[i]);
                        break;
                    case 1:
                        criteria.andEqualTo("twoCategoryId", categorys[i]);
                        break;
                    case 2:
                        criteria.andEqualTo("threeCategoryId", categorys[i]);
                        break;
                }
            }
        }
        criteria.andEqualTo("status", 0);
        List<Product> productList = listMapper.selectByExample(o);
        List<ProductStockVO> productStockVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productList)) {
            for (Product product : productList) {
                String pNum = product.getPNum();
                ProductStockVO productStockVO = new ProductStockVO();
                Example example = new Example(ProductStock.class);
                //查询出来商品编号
                example.createCriteria().andEqualTo("pNum", pNum);
                //查询库存表
                ProductStock productStock = productStockMapper.selectOneByExample(example);
                if (null != productStock) {
                    BeanUtils.copyProperties(product, productStockVO);
                    productStockVO.setStock(productStock.getStock());
                    productStockVOS.add(productStockVO);
                }
            }
        }
        PageInfo<Product> info = new PageInfo<>(productList);
        return new PageVO<>(info.getTotal(), productStockVOS);
    }

    //查询库存为了饼图数据
    @Override
    public List<ProductStockVO> findAllStocks(ProductVO productVO) {
        //模糊查询
        Example o = new Example(Product.class);
        //根据名称查询
        String name = productVO.getName();
        //根据分类查询
        Long[] categorys = productVO.getCategorys();

        Example.Criteria criteria = o.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (categorys != null && categorys.length > 0) {
            for (int i = 0; i < categorys.length; i++) {
                switch (i) {
                    case 0:
                        criteria.andEqualTo("oneCategoryId", categorys[i]);
                        break;
                    case 1:
                        criteria.andEqualTo("twoCategoryId", categorys[i]);
                        break;
                    case 2:
                        criteria.andEqualTo("threeCategoryId", categorys[i]);
                        break;
                }
            }
        }
        criteria.andEqualTo("status", 0);
        List<Product> products = listMapper.selectByExample(o);
        List<ProductStockVO> productStockList = products.stream().map(product -> {

            String pNum = product.getPNum();
            ProductStockVO productStockVO = new ProductStockVO();
            Example example = new Example(ProductStock.class);
            //查询出来商品编号
            example.createCriteria().andEqualTo("pNum", pNum);
            //查询库存表
            ProductStock productStock = productStockMapper.selectOneByExample(example);
            if (null != productStock) {
                BeanUtils.copyProperties(product, productStockVO);
                productStockVO.setStock(productStock.getStock());
            }
            return productStockVO;
        }).collect(Collectors.toList());
        return productStockList;
    }
}
